/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultarCategoria.java
 *
 * Created on 01/08/2011, 12:25:27
 */

package carrancao.gui;

import carrancao.controlador.Fachada;
import carrancao.entidades.Categoria;
import carrancao.entidades.Cliente;
import carrancao.util.CategoriaTableModel;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Lubnnia
 */
public class ConsultarCategoriaGUI extends javax.swing.JFrame {

    private List<Categoria> listaCategorias = new ArrayList<Categoria>();
    private CategoriaTableModel categoriaTableModel;

    /** Creates new form ConsultarCategoria */
    public ConsultarCategoriaGUI() {
        initComponents();
        setLocationRelativeTo(null);
        refreshTable(listaCategorias);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupPesquisa = new javax.swing.ButtonGroup();
        jTextFieldCampoConsulta = new javax.swing.JTextField();
        jRadioButtonCodigo = new javax.swing.JRadioButton();
        jRadioButtonNome = new javax.swing.JRadioButton();
        jButtonPesquisar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableCategoria = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextFieldCampoConsulta.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        buttonGroupPesquisa.add(jRadioButtonCodigo);
        jRadioButtonCodigo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jRadioButtonCodigo.setText("Código");

        buttonGroupPesquisa.add(jRadioButtonNome);
        jRadioButtonNome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jRadioButtonNome.setSelected(true);
        jRadioButtonNome.setText("Nome");

        jButtonPesquisar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButtonPesquisar.setText("Pesquisar");
        jButtonPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesquisarActionPerformed(evt);
            }
        });

        jTableCategoria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTableCategoria.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableCategoriaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableCategoria);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton1.setText("Fechar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jTextFieldCampoConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonCodigo)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonNome)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonPesquisar)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(231, 231, 231))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCampoConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonPesquisar)
                    .addComponent(jRadioButtonNome)
                    .addComponent(jRadioButtonCodigo))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limparCampos() {
        jTextFieldCampoConsulta.setText("");
    }

    private void refreshTable(List<Categoria> list) {
        categoriaTableModel = new CategoriaTableModel(list);
        jTableCategoria.setModel(categoriaTableModel);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButtonPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesquisarActionPerformed
        try {
            if(jRadioButtonCodigo.isSelected()) {
                Categoria categoria = null;
                categoria = Fachada.getInstance().getCategoriaControl().pesquisarCategoriaPorID(Integer.parseInt(jTextFieldCampoConsulta.getText()));

                if(categoria != null) {
                    listaCategorias.add(categoria);
                    refreshTable(listaCategorias);
                } else {
                    JOptionPane.showMessageDialog(null, "Código inválido!", "Mensagem do Sistema", JOptionPane.ERROR_MESSAGE);
                }
                limparCampos();

          } else if(jRadioButtonNome.isSelected()) {
              listaCategorias = Fachada.getInstance().getCategoriaControl().pesquisarCategoriaPorNome(jTextFieldCampoConsulta.getText());

              if(!(listaCategorias.isEmpty())) {
                  refreshTable(listaCategorias);
              } else {
                  JOptionPane.showMessageDialog(null, "Esta categoria não existe!", "Mensagem do Sistema", JOptionPane.ERROR_MESSAGE);
              }
              limparCampos();
          }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Nenhuma categoria encontrado!", "Mensagem do Sistema", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButtonPesquisarActionPerformed

    private void jTableCategoriaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableCategoriaMouseClicked
        if(evt.getClickCount() == 2) {
            int linha = jTableCategoria.getSelectedRow();

            AlterarCategoriaGUI tela = new AlterarCategoriaGUI();
            tela.setCategorias(categoriaTableModel.getCategoria(linha));
            tela.setVisible(true);
        }
    }//GEN-LAST:event_jTableCategoriaMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConsultarCategoriaGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupPesquisa;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonPesquisar;
    private javax.swing.JRadioButton jRadioButtonCodigo;
    private javax.swing.JRadioButton jRadioButtonNome;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableCategoria;
    private javax.swing.JTextField jTextFieldCampoConsulta;
    // End of variables declaration//GEN-END:variables

}
